package com.heyqing.topic.RomanToInt_0013;

import java.util.*;

/**
 * ClassName:RomanToInt
 * Package:com.heyqing.topic.RomanToInt_0013
 * Description:
 * 罗马数字转整数
 * <p>
 * I    V   X   L   C   D   M
 * 1    5   10  50  100 500 1000
 * IV   IX  XL  XC  CD  CM
 * 4    9   40  90  400 900
 *
 * @Date:2024/9/4
 * @Author:Heyqing
 */
public class RomanToInt {

    /**
     * 1、先判断是否为I、X、C
     * - 若不是换算值
     * - 若是2、
     * 2、看他的下一位是否合理
     *
     * @param s
     * @return
     */
    public int romanToInt(String s) {
        int result = 0;
        Map<String, Integer> referMap = generateMap();
        List<Character> judgeChar = Arrays.asList('I', 'X', 'C');
        for (int i = 0; i < s.length(); i++) {
            char currentChar = s.charAt(i);
            if (judgeChar.contains(currentChar)) {
                if (currentChar == 'I') {
                    if (i != s.length() - 1 && s.charAt(i + 1) == 'V') {
                        Integer num = referMap.get("IV");
                        result += num;
                        i++;
                    } else if (i != s.length() - 1 && s.charAt(i + 1) == 'X') {
                        Integer num = referMap.get("IX");
                        result += num;
                        i++;
                    } else {
                        Integer num = referMap.get(String.valueOf(currentChar));
                        result += num;
                    }
                } else if (currentChar == 'X') {
                    if (i != s.length() - 1 && s.charAt(i + 1) == 'L') {
                        Integer num = referMap.get("XL");
                        result += num;
                        i++;
                    } else if (i != s.length() - 1 && s.charAt(i + 1) == 'C') {
                        Integer num = referMap.get("XC");
                        result += num;
                        i++;
                    } else {
                        Integer num = referMap.get(String.valueOf(currentChar));
                        result += num;
                    }
                } else {
                    if (i != s.length() - 1 && s.charAt(i + 1) == 'D') {
                        Integer num = referMap.get("CD");
                        result += num;
                        i++;
                    } else if (i != s.length() - 1 && s.charAt(i + 1) == 'M') {
                        Integer num = referMap.get("CM");
                        result += num;
                        i++;
                    } else {
                        Integer num = referMap.get(String.valueOf(currentChar));
                        result += num;
                    }
                }
            } else {
                Integer num = referMap.get(String.valueOf(currentChar));
                result += num;
            }
        }
        return result;
    }

    private Map<String, Integer> generateMap() {
        return new HashMap<>() {{
            put("I", 1);
            put("V", 5);
            put("X", 10);
            put("L", 50);
            put("C", 100);
            put("D", 500);
            put("M", 1000);
            put("IV", 4);
            put("IX", 9);
            put("XL", 40);
            put("XC", 90);
            put("CD", 400);
            put("CM", 900);
        }};
    }

    public static void start() {
        RomanToInt romanToInt = new RomanToInt();
        System.out.println("请输入罗马数字：");
        Scanner sc = new Scanner(System.in);
        String romanNumber = sc.nextLine();
        int intNumber = romanToInt.romanToInt(romanNumber);
        System.out.println("罗马数字 " + romanNumber + " 的整数形式为：" + intNumber);
    }
}
